package com.bsoft.gol.hcs.specialist.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.spring.boot.annotation.SsdevDao;
import ctd.util.annotation.RpcService;
import ctd.util.converter.ConversionUtils;
import hcn.specialist.DocTeamFusAuthority;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import com.bsoft.gol.hcs.utils.CommonUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@SsdevDao( serviceId = "docTeamFusAuthorityDAOService")
public abstract class DocTeamFusAuthorityDAO extends HibernateSupportDelegateDAO<DocTeamFusAuthority> {
    /**
     * @param medicalTeamId the medicalTeamId
     * @author Qulc - 2022/5/24 19:49
     */
    @RpcService
    @DAOMethod(sql = "from DocTeamFusAuthority where medical_team_id =:medicalTeamId")
    public abstract List<DocTeamFusAuthority> queryDocTeamFusList(@DAOParam("medicalTeamId") String medicalTeamId);

    /**
     * 根据团队查询有权限的路径
     */
    @DAOMethod(sql = " from DocTeamFusAuthority where medical_team_id in :medicalTeamIds")
    public abstract List<DocTeamFusAuthority> queryAuthFus(@DAOParam("medicalTeamIds") List<Long> medicalTeamIds);


    @RpcService
    @DAOMethod(sql = "DELETE from DocTeamFusAuthority where medical_team_id =:medicalTeamId ")
    public abstract void deleteFusByTeamId(@DAOParam("medicalTeamId") Long medicalTeamId);


    @RpcService
    @DAOMethod(sql = "from DocTeamFusAuthority where medical_team_id =:medicalTeamId")
    public abstract List<DocTeamFusAuthority> queryTeamFusList(@DAOParam("medicalTeamId") Long medicalTeamId);

    @RpcService
    @DAOMethod(sql = "DELETE from DocTeamFusAuthority where medical_team_id =:medicalTeamId and route_id " +
            "in:teamFusAuthorityList")
    public abstract void deleteFusTeamList(@DAOParam("medicalTeamId") Long medicalTeamId, @DAOParam(
            "teamFusAuthorityList") List<Long> teamFusAuthorityList);

    @RpcService
    public DocTeamFusAuthority savDocTeamFusAuthority(DocTeamFusAuthority docTeamFusAuthority){
        return this.save(docTeamFusAuthority);
    }

    /**
     * 根据医生编码和团队类型查询有权限的路径
     * @param docId  医生主键
     * @param teamIdList
     * @return
     */
    public List<DocTeamFusAuthority> queryByDocId(final String docId, final List<Long> teamIdList) {
        if(StringUtils.isEmpty(docId)){
            return Collections.emptyList();
        }
        HibernateStatelessResultAction<List<DocTeamFusAuthority>> action =
                new AbstractHibernateStatelessResultAction<List<DocTeamFusAuthority>>() {
                    @Override
                    public void execute(StatelessSession ss) {
                        StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.append("SELECT a.fus_authority_id fusAuthorityId,a.medical_team_id " +
                                "docMedicalTeamId, " +
                                "a.org_id orgId, a.dept_id deptId, a.medical_team_id medicalTeamId, " +
                                "a.medical_team_name medicalTeamName, a.route_id routeId, " +
                                "a.route_name routeName, a.patient_type patientType, " +
                                "a.dept_type deptType, a.organization_Id organizationId, " +
                                "a.organization_name organizationName, a.superior_department_id " +
                                "superiorDepartmentId, " +
                                "a.superior_department_name superiorDepartmentName, a.department_name departmentId, " +
                                "a.department_id departmentName " +
                                "FROM doc_team_fus_authority AS a  " +
                                "WHERE 1=1 ");
                        if(CommonUtils.isNotEmpty(teamIdList)){
                            stringBuilder.append(" AND a.medical_team_id in(:teamIdList)");
                        }


                        Query query = ss.createSQLQuery(stringBuilder.toString());
                        if(CommonUtils.isNotEmpty(teamIdList)){
                            query.setParameterList("teamIdList",teamIdList);
                        }
                        query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                        List<Map> list = query.list();
                        List<DocTeamFusAuthority> voList = new ArrayList<>();
                        if (CommonUtils.isNotEmpty(list)) {
                            for (Map map : list) {
                                voList.add(ConversionUtils.convert(map, DocTeamFusAuthority.class));
                            }
                        }
                        setResult(voList);
                    }
                };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }
}
